/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "SqlOperation.h"
#include "SqlConnection.h"
#include "SqlPreparedStatement.h"

namespace UGame {
namespace Data {
namespace Rel {

    SqlPreparedRequest::SqlPreparedRequest(int _Index, SqlStmtParameters* _Arg)
        : _index(_Index), _param(_Arg)
    {
    }

    SqlPreparedRequest::~SqlPreparedRequest()
    {

    }

    bool SqlPreparedRequest::Execute(SqlConnection* _Conn)
    {
        SqlConnection::Lock _Guard(_Conn);
        return _Conn->ExecuteStmt(_index, *_param);
    }

    void SqlResultQueue::Update(void)
    {
        IQueryCallback* _Callback = NULL;
        while (Next(_Callback))
        {
            std::auto_ptr<IQueryCallback> _CBPtr(_Callback);
            _CBPtr->Execute();
        }
    }

    bool SqlQuery::Execute(SqlConnection* _Conn)
    {
        if (!_callback || !_queue) {
            return false;
        }

        SqlConnection::Lock _Guard(_Conn);
        _callback->SetResult(_Conn->Query(_sql));
        _queue->Add(_callback);
        return true;
    }

}}}